// pages/materialApplyRecord/materialApplyRecord.js
import {materialApplyRecoad} from "@/api/material"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedDate: '',
    currentDate: '',
    searchKeyword: '',
    records: [],//列表
    totalQuantity: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 初始化日期
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const formattedDate = `${year}-${month}`;
    console.log(formattedDate);
    this.setData({
      selectedDate: formattedDate,
    });
    
    // 初始化示例数据
    this.initRecords({userId:wx.getStorageSync('id'),assignTime:formattedDate,materialName:''});
  },

  // 初始化记录数据
  async initRecords(data) {
    const result=await materialApplyRecoad(data)
    if(result.code==200){
      this.setData({
        records: result.data.list,
        totalQuantity:result.data.total
      });
    }
  },

  // 日期变更处理
  onDateChange(e) {
    const selectedDate = e.detail.value;
    console.log(selectedDate);
    this.setData({
      selectedDate
    },()=>{
      this.initRecords({userId:wx.getStorageSync('id'),assignTime:selectedDate,materialName:''})
    });
  },

  // 搜索输入处理
  onSearchInput(e) {
    const searchKeyword = e.detail.value;
    this.setData({ searchKeyword });
    
    // 防抖处理
    if (this.searchTimer) clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.initRecords({userId:wx.getStorageSync('id'),assignTime:'',materialName:searchKeyword})
    }, 300);
  },

})